package com.ycb.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycb.common.Result;
import com.ycb.dto.SetmealDto;
import com.ycb.pojo.Setmeal;
import com.ycb.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author ycb
 * @since 2024-09-30
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name) throws Exception {
        return setmealService.pageSetmealDto(page,pageSize,name);
    }

    /**
     * 保存套餐信息
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return Result.success("套餐添加成功");
    }

    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("要删除的套餐id为：{}", ids);
        setmealService.removeWithDish(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable String status, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, ids);
        updateWrapper.set(Setmeal::getStatus, status);
        setmealService.update(updateWrapper);
        return Result.success("批量操作成功");
    }

    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable Long id) {

        SetmealDto setmealDto = setmealService.getDtoById(id);
        return Result.success(setmealDto);
    }

    @PutMapping
    public Result<String> updateWithDish(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return Result.success("更新套餐信息成功");
    }
}
